001    /*
002     * Copyright 2004 Stephen J. McConnell.
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.metro.info;
020    
021    import java.io.Serializable;
022    
023    import net.dpml.component.Directive;
024    
025    /**
026     * A <code>PartReference</code> is a serializable object that contains a key and 
027     * an associated part.
028     *
029     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
030     * @version 1.0.1
031     */
032    public class PartReference implements Serializable, Comparable
033    {
034       /**
035        * Serial version identifier.
036        */
037        static final long serialVersionUID = 1L;
038    
039        /**
040         * The key.
041         */
042        private final String m_key;
043    
044        /**
045         * The supplied argument.
046         */
047        private final Directive m_directive;
048    
049        /**
050         * The reference priority.
051         */
052        private final int m_priority;
053    
054        /**
055         * Creation of a new part reference.
056         *
057         * @param key the key identifying this part within the scope of its container
058         * @param directive the directive
059         */
060        public PartReference( final String key, Directive directive )
061        {
062            this( key, directive, 0 );
063        }
064        
065        /**
066         * Creation of a new part reference.
067         *
068         * @param key the key identifying this part within the scope of its container
069         * @param directive the directive
070         * @param priority the relative priority
071         */
072        public PartReference( final String key, Directive directive, int priority )
073        {
074            if( null == key )
075            {
076                throw new NullPointerException( "key" );
077            }
078            if( null == directive )
079            {
080                throw new NullPointerException( "directive" );
081            }
082            m_key = key;
083            m_directive = directive;
084            m_priority = priority;
085        }
086    
087        /**
088         * Return the key.
089         * @return the key
090         */
091        public String getKey()
092        {
093            return m_key;
094        }
095    
096        /**
097         * Return the directive.
098         * @return the directive
099         */
100        public Directive getDirective()
101        {
102            return m_directive;
103        }
104    
105        /**
106         * Return the priority value.
107         * @return the priority ranking of this reference 
108         */
109        public int getPriority()
110        {
111            return m_priority;
112        }
113        
114       /**
115        * Compare this object with the supplied object.
116        * @param other the object to compare with
117        * @return the result
118        */
119        public int compareTo( Object other )
120        {
121            if( null == other )
122            {
123                throw new NullPointerException( "other" );
124            }
125            else if( other instanceof PartReference )
126            {
127                PartReference ref = (PartReference) other;
128                Integer p1 = new Integer( m_priority );
129                Integer p2 = new Integer( ref.m_priority );
130                return p1.compareTo( p2 );
131            }
132            else
133            {
134                String suspect = other.getClass().getName();
135                throw new IllegalArgumentException( suspect );
136            }
137        }
138        
139       /**
140        * Test if the supplied object is equal to this object.
141        * @param other the object to compare with this instance
142        * @return TRUE if the supplied object is equal to this object
143        */
144        public boolean equals( Object other )
145        {
146            if( null == other )
147            {
148                return false;
149            }
150            else
151            {
152                if( !( other instanceof PartReference ) )
153                {
154                    return false;
155                }
156                else
157                {
158                    PartReference reference = (PartReference) other;
159                    if( !m_key.equals( reference.getKey() ) )
160                    {
161                        return false;
162                    }
163                    else if( !m_directive.equals( reference.m_directive ) )
164                    {
165                        return false;
166                    }
167                    else
168                    {
169                        return m_priority == reference.m_priority;
170                    }
171                }
172            }
173        }
174    
175       /**
176        * Return the hashcode for the instance.
177        * @return the instance hashcode
178        */
179        public int hashCode()
180        {
181            int hash = m_key.hashCode();
182            hash ^= m_directive.hashCode();
183            hash ^= m_priority;
184            return hash;
185        }
186        
187       /**
188        * Return a string representation of the instance.
189        * @return the string representation
190        */
191        public String toString()
192        {
193            return "[reference: key=" + m_key + "directive=" + m_directive + "]";
194        }
195    }